<template>
	<el-dialog
		v-if="m"
		:title="m.id == 0 ? '数据增加' : '数据修改'"
		:visible.sync="isShow"
		width="400px"
		top="25vh"
		:append-to-body="true"
		:destroy-on-close="true"
		:close-on-click-modal="false"
		custom-class="full-dialog"
		>
		<div class="vue-box">
			<div class="c-panel">
				<!-- 参数栏 -->
				<br>
				<el-form size="mini" :model="m" ref="m" :rules="rules" label-width="100px">
					<el-form-item label="机构名称：" prop="orgname">
						<el-input v-model="m.orgname"></el-input>
					</el-form-item>
					<el-form-item label="机构地址：" prop="orgaddress">
						<el-input v-model="m.orgaddress"></el-input>
					</el-form-item>
					<el-form-item label="机构描述：" prop="orgdesc">
						<el-input v-model="m.orgdesc"></el-input>
					</el-form-item>
				</el-form>
			</div>
		</div>
		<span slot="footer" class="dialog-footer">
			<el-button size="small" @click="isShow = false">取 消</el-button>
			<el-button size="small" type="primary" @click="ok()">确 定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default{
		data(){
			var checkorgname = (r,v,c)=>{
				if(this.$data.ist == 0){
					if(v === ''){
						return c(new Error('机构名称不能为空'));
					}else{
						this.$axios.get("api/sysOrganizations/checkName/"+v).then(function (res){
							if(res.data.data.length > 0){
								return c(new Error("机构已存在，请更换!!!"));
							}else{
								c();
							}
						});
					}
				}else{
					c();
				}
				
			}
			return {
				isShow: false,
				m: {id:0},
				ist:true,
				rules:{
					orgname:[
						{validator:checkorgname,trigger: 'blur'}
					],
					orgaddress:[
						{ required: true, message: '请输入部门地址', trigger: 'blur' },
						{ min: 4, max: 100, message: '长度在 4 到 100 个字符', trigger: 'blur' }
					],
					orgdesc:[
						{ required: true, message: '请输入部门描述', trigger: 'blur' },
						{ min: 10, max: 100, message: '长度在 10 到 100 个字符', trigger: 'blur' }
					]
				}
			}
		},
		methods:{
			open:function (t){
				this.isShow = true;
				this.ist = t == 0 ? false:true;
				this.create_m(t);
			},
			create_m:function (t){
				//返回添加时有可能用到的数据,如果没有此位置可以为空
				if(t == 0){
					this.m = {
						orgname:'',
						orgaddress:'',
						orgdesc:''
					};
					return ;//判断 完成后直接结束方法，省略if else结构
				}
				var than = this;
				//修改时用于查一操作
				console.log(t);
				this.$axios.get("/api/sysOrganizations/"+t).then(function(res){
					than.m = res.data.data;
				});
			},
			//点击提交 时，用来判断是添加操作，还是修改操作
			ok:function (){
				let than = this;
				this.$refs['m'].validate((valid) => {
					if(valid){
						console.log(this.$data.ist == false)
						if(this.$data.ist == false){
							this.$axios.post("/api/sysOrganizations/",this.$data.m).then(function (res){
								if(res.data.data.id != null){
									than.sa.msg(res.data.msg,{icon:1,time:500});
									than.$data.isShow = false;
									than.$parent.init();
								}
							});
						}else{
							this.$axios.put("/api/sysOrganizations/"+this.$data.m.id,this.$data.m).then(function (res){
								//if(res.data.data.id != null){
									than.sa.msg(res.data.msg,{icon:1,time:500});
									than.$data.isShow = false;
									than.$parent.init();
								//}
							});
						}
					}else{
						console.log('==>');
					}
				});
			}
		}
	}
</script>

<style scoped="scoped">
	.c-panel{margin: 0px; padding: 0px 20px;}
</style>
